<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" size="small" label-width="100px">
      <el-form-item label="输入搜索：">
        <el-input v-model="listQuery.keyword" class="input-width" placeholder="角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearchList" size="small"> 搜索 </el-button>
        <el-button @click="handleResetSearch" size="small"> 重置 </el-button>
        <el-button type="primary" size="small" class="btn-add" @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="roleTable" :data="list" height="100%" style="width: 100%" v-loading="listLoading" border>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="角色名称" minWidth="100" align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="描述" minWidth="160" show-overflow-tooltip align="center">
          <template #default="scope">{{ scope.row.description }}</template>
        </el-table-column>
        <el-table-column label="用户数" width="90" align="center">
          <template #default="scope">{{ scope.row.adminCount }}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="160" align="center">
          <template #default="scope">{{ formatDateTime(scope.row.createTime) }}</template>
        </el-table-column>
        <el-table-column label="是否启用" width="100" align="center">
          <template #default="scope">
            <el-switch
              size="small"
              @change="handleStatusChange(scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleUpdate(scope.row)"> 编辑 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)"> 删除 </el-button>
            <el-button type="primary" size="small" link @click="handleSelectMenu(scope.row)"> 分配菜单 </el-button>
            <el-button type="primary" size="small" link @click="handleSelectResource(scope.row)"> 分配资源 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        v-model:current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :title="role.id ? '编辑角色' : '添加角色'" v-model="dialogVisible" width="40%">
      <el-form :model="role" ref="roleForm" label-width="auto" size="small">
        <el-form-item label="角色名称：">
          <el-input v-model="role.name"></el-input>
        </el-form-item>
        <el-form-item label="描述：">
          <el-input v-model="role.description" type="textarea" :rows="5"></el-input>
        </el-form-item>
        <el-form-item label="是否启用：">
          <el-radio-group v-model="role.status">
            <el-radio :value="1">是</el-radio>
            <el-radio :value="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="handleDialogConfirm" size="small">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { fetchList, createRole, updateRole, updateStatus, deleteRole } from '@/api/role'
import type { Role, RolePageDto } from '@/models/role'
import { ArrowDown } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { formatDateTime } from '@/utils/format'

defineOptions({ name: 'RoleList' })
const router = useRouter()
const listQuery = ref<RolePageDto>({ pageNum: 1, pageSize: 10 })
const list = ref<Role[]>([])
const total = ref(0)
const listLoading = ref(false)
const dialogVisible = ref(false)
const role = ref<Role>({})
getList()

function handleResetSearch() {
  listQuery.value = { pageNum: 1, pageSize: 10 }
}

function handleSearchList() {
  listQuery.value.pageNum = 1
  getList()
}

function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}

function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}

function handleAdd() {
  dialogVisible.value = true
  role.value = { adminCount: 0, status: 1 }
}

async function handleStatusChange(row: Role) {
  row.id && (await updateStatus(row.id, { status: row.status as number }))
  ElMessage.success('修改成功')
}

async function handleDelete(row: Role) {
  await ElMessageBox.confirm('是否要删除该角色?', '提示', { type: 'warning' })
  await deleteRole({ ids: row.id + '' })
  ElMessage.success('删除成功！')
  getList()
}

function handleUpdate(row: Role) {
  dialogVisible.value = true
  role.value = { ...row }
}

async function handleDialogConfirm() {
  if (role.value.id) {
    await updateRole(role.value.id, role.value)
    ElMessage.success('添加成功！')
  } else {
    await createRole(role.value)
    ElMessage.success('添加成功！')
  }
  dialogVisible.value = false
  getList()
}

function handleSelectMenu(row: Role) {
  router.push({ path: '/ums/allocMenu', query: { roleId: row.id } })
}

function handleSelectResource(row: Role) {
  router.push({ path: '/ums/allocResource', query: { roleId: row.id } })
}

async function getList() {
  listLoading.value = true
  try {
    const response = await fetchList(listQuery.value)
    list.value = response.data.data.list
    total.value = response.data.data.total
  } finally {
    listLoading.value = false
  }
}
</script>
<style></style>
